using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;


namespace Kopytko
{
    /// <summary>
    /// Attached property dla Grida
    /// 
    /// w danym gridzie wstawia kreciol, lub go usuwa
    /// w zaleznosci od BusyState
    /// 
    /// Uzywac jako AttachedProperty lub statycznej metody SetBusyState
    /// </summary>
	public static class BusyIndicatorBehavior
	{
		#region Attached Properties

		public static readonly DependencyProperty BusyStateProperty = DependencyProperty.RegisterAttached("BusyState", typeof(bool),
		                                                                                                  typeof(BusyIndicatorBehavior),
		                                                                                                  new UIPropertyMetadata(false, OnBusyStateChanged));


		public static readonly DependencyProperty TargetVisualProperty = DependencyProperty.RegisterAttached("TargetVisual", typeof(UIElement),
		                                                                                                     typeof(BusyIndicatorBehavior),
		                                                                                                     new UIPropertyMetadata(null));


		public static UIElement GetTargetVisual(DependencyObject obj)
		{
			return (UIElement) obj.GetValue(TargetVisualProperty);
		}

		public static void SetTargetVisual(DependencyObject obj, UIElement value)
		{
			obj.SetValue(TargetVisualProperty, value);
		}

		public static bool GetBusyState(DependencyObject obj)
		{
			return (bool) obj.GetValue(BusyStateProperty);
		}

		public static void SetBusyState(DependencyObject obj, bool value)
		{
			obj.SetValue(BusyStateProperty, value);
		}

		#endregion

		#region Implementation


		private static void OnBusyStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			bool isBusy = (bool) e.NewValue;
			bool wasBusy = (bool) e.OldValue;

			if (isBusy == wasBusy)
			{
				return;
			}

			var hostGridObject = (GetTargetVisual(d) ?? d);
			Debug.Assert(hostGridObject != null);

			var hostGrid = hostGridObject as Grid;

			if (isBusy)
			{
				Debug.Assert(LogicalTreeHelper.FindLogicalNode(hostGrid, "BusyIndicator") == null);

			    var pb = new CircularProgressBar()
			                 {
			                     Name = "BusyIndicator",
			                     HorizontalAlignment = HorizontalAlignment.Center,
			                     VerticalAlignment = VerticalAlignment.Center,
			                 };

				hostGrid.Children.Add(pb);

                if (hostGrid.RowDefinitions.Count>0)
                    Grid.SetRowSpan(pb, hostGrid.RowDefinitions.Count);
                if (hostGrid.ColumnDefinitions.Count>0)
                Grid.SetColumnSpan(pb, hostGrid.ColumnDefinitions.Count);
                
			}
			else
			{
                var pb = (CircularProgressBar)LogicalTreeHelper.FindLogicalNode(hostGrid, "BusyIndicator");

				Debug.Assert(pb != null);

				if (pb != null)
				{
					hostGrid.Children.Remove(pb);
				}
			}
		}


		#endregion
	}
}